import time

from views.base import BaseHandler
from db import db
import json,datetime

class landmarks(BaseHandler):
    def get(self):
        userid = self.get_query_argument("userid")
        page = self.get_query_argument("page")
        page_size = self.get_query_argument("page_size")
        start = (int(page)-1) * int(page_size)
        sql = "select count(*) as a from (select * from landmarks where audit_userid=%d union all select * from landmarks1 where audit_userid=%d union all select * from landmarks2 where audit_userid=%d) as t" % (
        int(userid), int(userid), int(userid))
        ress = db.findone(sql)
        total = ress['a']
        sqls = "select *  from (select * from landmarks where status=0 and audit_userid=%d union all select * from landmarks1 where status=0 and audit_userid=%d union all select * from landmarks2 where status=0 and audit_userid=%d) as t limit %d,%d" % (
            int(userid), int(userid), int(userid), int(start), int(page_size))
        res = db.findAll(sqls)
        for i in res:
            i["interest_rate"] = float(i["interest_rate"])
            i["code"] = str(i["code"])
            i["audit_time"] = datetime.datetime.strftime(i["audit_time"],"%Y-%m-%d %H:%M:%S")
            if i["duedate"]:
                i["duedate"] = datetime.datetime.strftime(i["duedate"],"%Y-%m-%d %H:%M:%S")
        return self.finish({"code": 200,"msg":"获取成功","data":res,"total":total})
    def post(self):
        code = json.loads(self.request.body)
        number = hash(int(code)) % 3
        audit_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        print(number)
        if number !=0:
            landmarks = "landmarks" + str(number)
            sql = "update %s set status=%d,audit_time='%s' where code=%d"%(landmarks,1,audit_time,int(code))
            print(sql)
            db.add(sql)
            db.commit()
        else:
            sql1 = "update landmarks set status=%d,audit_time='%s' where code=%d" % (1, audit_time, int(code))
            print("sql1",sql1)
            db.add(sql1)
            db.commit()
        return self.finish({"code": 200, "msg": "审核成功"})

